package net.bwie.realtime.jtp.log.function;

import net.bwie.realtime.jtp.common.utils.DateTimeUtil;
import net.bwie.realtime.jtp.log.bean.PageViewBean;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

/**
 * 简介说明:
 *
 * @author: LiLi
 * @date: 2025/05/20 15:59:54
 * @version: 1.0
 */
public class PageViewReportWindowFunction implements WindowFunction<PageViewBean, String, String, TimeWindow> {
    @Override
    public void apply(String key, TimeWindow window, Iterable<PageViewBean> input, Collector<String> collector) throws Exception {
        /*
       key：表示分组字段；window：表示时间窗口，可以获取窗口开始和结束时间；input：表示窗口中所有数据
        */
        // 1窗口时间
        String windowStart = DateTimeUtil.convertLongToString(window.getStart(), "yyyy-MM-dd HH:mm:ss");
        String windowEnd = DateTimeUtil.convertLongToString(window.getEnd(), "yyyy-MM-dd HH:mm:ss");

        // 2获取窗口中计算结果
        PageViewBean bean = input.iterator().next();
        long pvCount = bean.getPvCount();
        long pvDuringTime = bean.getPvDuringTime();
        long uvCount = bean.getUvCount();
        long sessionCount = bean.getSessionCount();

        // 3输出
        String output = windowStart + "," + windowEnd + "," + key
                + "," + pvCount + "," + pvDuringTime + "," + uvCount + "," + sessionCount
                + "," + System.currentTimeMillis();

        collector.collect(output);
    }
}
